package com.hb.umsp.contraller;

import com.github.pagehelper.PageInfo;
import com.hb.umsp.entity.HbAttendance;
import com.hb.umsp.service.AttendanceService;
import com.hb.umsp.util.ExcelUtils;
import com.hb.umsp.util.JsonResult;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;

@CrossOrigin("*")
@RestController
@RequestMapping("/excel")
public class AttendanceController {

    private static final Logger log = LogManager.getLogger(AttendanceController.class);

    @Autowired
    AttendanceService attendanceService;

    /**
     * Excel导出
     * @param response
     */
    @GetMapping("/exportExcel")
    public void exportExcel(HttpServletRequest request,HttpServletResponse response){
        //使用假数据代替从数据库查出来的需要导出的数据
        List<HbAttendance> empList = handleRepositoryData();
        long t1 = System.currentTimeMillis();
        ExcelUtils.writeExcel(request,response, empList, HbAttendance.class);
        long t2 = System.currentTimeMillis();
        System.out.println(String.format("write over! cost:%sms", (t2 - t1)));
    }

    /**
     * Excel导入
     * @param file
     */
    @PostMapping("/import")
    public JsonResult<String> readExcel(@RequestBody MultipartFile file){
        long t1 = System.currentTimeMillis();
        log.info("上传的文件："+file);
        List<HbAttendance> list = ExcelUtils.readExcel("", HbAttendance.class, file);
        long t2 = System.currentTimeMillis();
        log.info(String.format("read over! cost:%sms", (t2 - t1)));
        attendanceService.insert(list);

        return new JsonResult<>("20000","导入成功");
    }

    public List<HbAttendance> handleRepositoryData() {
        List<HbAttendance> empList = new ArrayList<>();
        HbAttendance emp;
        for (int i = 1; i<= 10; i++) {
            emp = new HbAttendance();
//            emp.setId(i);
//            emp.setEmpName("员工" + i);
//            emp.setEmpNo((1000 + i) + "");
//            emp.setJob("JY" + i);
//            emp.setSalary(new BigDecimal(i * 1000 + ""));
//            emp.setEntryTime(LocalDateTime.now().minusHours(Long.valueOf(i)));
            empList.add(emp);
        }
        return empList;
    }

    /**
     * 前台页面的数据列表
     * @return
     */
    @RequestMapping("/getList")
    public JsonResult getList(@RequestParam(defaultValue = "1") int pageNum,
                              @RequestParam(defaultValue = "5" ) int pageSize,
                              HttpServletRequest request,
                              @RequestBody(required = false) HbAttendance hbAttendance){

        String token = request.getHeader("token");
        if (token == null || "".equals(token)) {
            return new JsonResult("50008","未获取token");
        }
        PageInfo<HbAttendance> page = attendanceService.findByPage(pageNum,pageSize,token,hbAttendance);

        return new JsonResult("20000",page);
    }

    @RequestMapping("/delete")
    public JsonResult deleteByMonth(@RequestBody HbAttendance hbAttendance){

        attendanceService.deleteAttendance(hbAttendance);

        return new JsonResult("20000","删除成功");
    }
}
